package com.newway.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.TeachingEntity;
import com.newway.back.mapper.TeachingMapper;
import com.newway.common.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/system/teaching")
public class TeachingController {

    @Resource
    TeachingMapper teachingMapper;

    @GetMapping("/getList")
    public Result getList(TeachingEntity teachingEntity){
        Page<TeachingEntity> teachingEntityPage = new Page<>(teachingEntity.getPageNum(),teachingEntity.getPageSize());
        QueryWrapper<TeachingEntity> teachingEntityQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(teachingEntity.getTitle())){
            teachingEntityQueryWrapper.like("title",teachingEntity.getTitle());
        }
        if(StringUtils.isNotBlank(teachingEntity.getCategory())){
            teachingEntityQueryWrapper.eq("category",teachingEntity.getCategory());
        }
        if(StringUtils.isNotBlank(teachingEntity.getLevel())){
            teachingEntityQueryWrapper.eq("level",teachingEntity.getLevel());
        }
        teachingEntityQueryWrapper.orderByDesc("create_time");
        Page<TeachingEntity> result = teachingMapper.selectPage(teachingEntityPage, teachingEntityQueryWrapper);
        return Result.success(result);
    }

    @PostMapping("/addTeaching")
    public  Result addTeaching(@RequestBody TeachingEntity teachingEntity){
        teachingEntity.setCreateTime(LocalDateTime.now());
        teachingMapper.insert(teachingEntity);
        return Result.success();
    }

    @PostMapping("/updateTeaching")
    public  Result updateTeaching(@RequestBody TeachingEntity teachingEntity){
        teachingMapper.updateById(teachingEntity);
        return Result.success();
    }

    @PostMapping("/delTeaching/{id}")
    public  Result delTeaching(@PathVariable("id")Integer id){
        teachingMapper.deleteById(id);
        return Result.success();
    }

}
